import { _decorator, Component, director, Node, UITransform } from 'cc';
import { events } from './Enums';
import { Global } from './Global';
import { Tools } from './Tools';
import { BtnAction } from './BtnAction';
const { ccclass, property } = _decorator;

@ccclass('GuideAction')
export class GuideAction extends Component {
    @property({ type: Node })
    guide: Node = null;

    private show_time: number = 0;
    private touch_time: number = 0;
    private guide_1_times: number = 0;

    start() {
        this.default_pos();
        director.on(events.check_guide, this.check_guide_1, this);
        this.schedule(this.check_guide_2, 10);
    }

    onDestroy() {
        director.off(events.check_guide, this.check_guide_1, this);
        this.unschedule(this.check_guide_2);
    }

    action_lvl_start_begin() {
        this.show_time = 0;
        this.touch_time = 0;
        this.guide_1_times = 0;
        this.default_pos();
    }

    check_guide_2() {
        const button_arr = Global.layer_btn_action.get_button_arr();
        const arr = [];
        const slot_pin_arr = Global.layer_empty_action.get_pin_arr();
        const slot_lock_num = Global.layer_empty_action.get_lock_num();

        button_arr.forEach(btn => {
            const ba = btn.getComponent(BtnAction);
            if (!ba) return;
            switch (ba.index) {
                case 1:
                    if (slot_lock_num > 0) arr.push(btn);
                    break;
                case 2:
                    arr.push(btn);
                    break;
                case 3:
                    if (slot_pin_arr.length > 0) arr.push(btn);
                    break;
                default:
                    arr.push(btn);
            }
        });

        try {
            const locked_cat_arr = Global.layer_cat_action.get_locked_cat();
            if (locked_cat_arr.length > 0) {
                arr.push(locked_cat_arr[0]);
            }
            if (arr.length === 0) return;
            const r = Tools.random_between_floor(0, arr.length - 1);
            this.show_guide(arr[r]);
        } catch (error) {
            console.error("check_guide_2 error:", error);
        }
    }

    check_guide_1() {
        this.touch_time = Date.now();
        if (this.guide_1_times > 2 && (this.touch_time - this.show_time) < 30000) {
            return;
        }

        const locked_cat_arr = Global.layer_cat_action.get_locked_cat();
        if (locked_cat_arr.length === 0) return;

        let show = false;
        const slot_pin_arr = Global.layer_empty_action.get_pin_arr();

        if (this.show_time === 0 && slot_pin_arr.length === 2 && Global.cur_lvl === 1) {
            show = true;
        }
        if (slot_pin_arr.length >= 4) {
            show = true;
        }
        if (show) {
            this.guide_1_times++;
            this.show_time = Date.now();
            this.show_guide(locked_cat_arr[0]);
        }
    }

    private show_guide(target: Node) {
        if (!this.guide || !target) return;

        const world_pos = target.getWorldPosition();
        const local_pos = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
        this.guide.setPosition(local_pos);
        this.scheduleOnce(() => {
            this.default_pos();
        }, 3);
    }

    private default_pos() {
        if (this.guide) {
            this.guide.setPosition(0, -2000, 0);
        }
    }
}
